package hot100;

import static hot100.ListNode.generateList;

import java.util.List;

public class Solution92 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode prev = dummyNode.next;
        for (int i = 0; i < left - 1; i++) {
            prev = prev.next;
        }
        ListNode cur = prev.next;
        ListNode next;
        for (int i = 0; i < right - left; i++) {
            next = cur.next;
            cur.next = next.next;
            next.next = prev.next;
            prev.next = next;
        }
        return dummyNode.next;
    }

    public static void main(String[] args) {
        Solution92 solution92 = new Solution92();
        ListNode head = generateList(new int[]{1, 2, 3, 4, 5});
        solution92.reverseBetween(head, 2, 4);
    }
}
